package gwtappcontainer.server.apis.insight;

import gwtappcontainer.server.apis.admin.GateKeeper;
import gwtappcontainer.server.apis.admin.Roles;
import gwtappcontainer.shared.apis.APIResponse;
import gwtappcontainer.shared.apis.APIResponse.Status;
import gwtappcontainer.shared.apis.insight.CenterProp;
import gwtappcontainer.shared.apis.insight.ProgramTypeProp;

import java.util.List;

import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;

@Api(name = "insight", 
	scopes = { "https://www.googleapis.com/auth/userinfo.email" }
)
public class InsightAPI {
	
	private GateKeeper gateKeeper = new GateKeeper();
	
	@ApiMethod(httpMethod = HttpMethod.GET, path = "listprogramtypes")
	public APIResponse listProgramTypes() {
		try {
			ProgramTypeRepository repository = new ProgramTypeRepository();
			
			List<ProgramTypeProp> all = repository.list();						
			
			APIResponse resp = new APIResponse(Status.SUCCESS, all);
			return resp;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.PUT, path = "addprogramtype")
	public APIResponse addProgramType(@Named("program_type") String name, User user) {
		try {
			
			gateKeeper.ensureRole(user, Roles.Role.PORTAL_ADMIN);
			
			ProgramTypeRepository repository = new ProgramTypeRepository();
			
			ProgramTypeProp prop = repository.add(name);
			
			APIResponse resp = new APIResponse(Status.SUCCESS, prop);
			return resp;
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.POST, path = "updateprogramtype")
	public APIResponse updateProgramType(@Named("new_name") String newName,
			@Named("existing_name") String existingName, User user) {
		
		try {
			
			gateKeeper.ensureRole(user, Roles.Role.PORTAL_ADMIN);
			
			ProgramTypeRepository repository = new ProgramTypeRepository();
			
			ProgramTypeProp prop = repository.update(newName, existingName);												
			
			APIResponse resp = new APIResponse(Status.SUCCESS, prop);
			return resp;
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.GET, path = "listcenters")
	public APIResponse listCenters() {
		try {
			CenterRepository repository = new CenterRepository();
			
			List<CenterProp> all = repository.list();						
			
			APIResponse resp = new APIResponse(Status.SUCCESS, all);
			return resp;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.PUT, path = "addcenter")
	public APIResponse addCenter(@Named("center") String name, User user) {
		try {
			
			gateKeeper.ensureRole(user, Roles.Role.PORTAL_ADMIN);
			
			CenterRepository repository = new CenterRepository();
			
			CenterProp prop = repository.add(name);
			
			APIResponse resp = new APIResponse(Status.SUCCESS, prop);
			return resp;
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(httpMethod = HttpMethod.POST, path = "updatecenter")
	public APIResponse updateCenter(@Named("new_name") String newName,
			@Named("existing_name") String existingName, User user) {
		
		try {
			
			gateKeeper.ensureRole(user, Roles.Role.PORTAL_ADMIN);
			
			CenterRepository repository = new CenterRepository();
			
			CenterProp prop = repository.update(newName, existingName);												
			
			APIResponse resp = new APIResponse(Status.SUCCESS, prop);
			return resp;
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
}
